import random

user_lib = {
    1314:{
        '账号':1314,
        '账户类型':"金卡",
        '姓名':"Mr.刘",
        '密码':"666666",
        '地址':"中国，河南，郑州，高新区，863",
        '存款余额':100000,
        '开户行':"中国农业银行的昌平沙河支行"
    },
    1412:{
        '账号':1412,
        '账户类型':"金卡",
        '姓名':"Mr.刘",
        '密码':"666666",
        '地址':"中国，河南，郑州，高新区，863",
        '存款余额':200000,
        '开户行':"中国农业银行的昌平沙河支行"
    }
}

# 开户模块  option == '1'
def kaihu():
    global user_lib
    username = random.randint(10000000,100000000)
    account_type = ["金卡", "普通卡"]
    while True:
        try:
            print('输入 0 为【金卡】：最大转账额为5万，转出最大5万，转入没限制')
            print('输入 1 为【普通卡】：最大转账额为2万，转出最大2万，转入没限制')
            a_type = input("请输入您要开的账户类型：")
            if a_type not in ["0", "1"]:
                print("输入错误，请重新输入！")
                continue
            a_type = account_type[int(a_type)]
            break
        except ValueError:
            print("输入不合法！")
            continue


    name = input("请输入您的姓名：")

    while True:
        try:
            password = input("请输入您的密码（6位数字）：")
            if not password.isdigit() or len(password) != 6:
                print("请输入6位数字的密码。")
                continue
            break
        except ValueError:
            print("输入不合法")

    country = input("请输入您的国家：")
    province = input("请输入您的省份：")
    street = input("请输入您的街道：")
    house = input("请输入您的门牌号：")

    money = 100000
    bank = "中国农业银行的昌平沙河支行"

    user = {
        '账号':username,
        '账户类型':a_type,
        '姓名':name,
        '密码':password,
        '地址':f"{country},{province},{street},{house}",
        '存款余额':money,
        '开户行':bank
    }



    if username in user_lib.keys():
        print("用户已存在。")
    elif len(user_lib) >= 100:
        print("用户库已满")
    else:
        user_lib[username] = user
        print(f"您的账号是：{username}，请妥善保管您的账号。")

    print(user_lib)

# 存钱模块 option == '2'
def set_money(username):

    if username in user_lib.keys():
        money_set = int(input("请输入存款的金额："))
        user_lib.get(username)["存款余额"] += money_set
        print("存款成功。")
        print(f'当前账号存款余额为：{user_lib.get(username)["存款余额"]}')
    elif username not in user_lib.keys():
        print('False，账号不存在。')

# 取钱模块
def get_money(username):
    if username in user_lib.keys():
        password = input("请输入密码：")
        if password != user_lib.get(username)["密码"]:
            print("密码不对。")
        else:
            money_get = int(input("请输入取款的金额："))
            if money_get > user_lib.get(username)["存款余额"]:
                print("钱不够。")
                return
            user_lib.get(username)["存款余额"] -= money_get
            print("取款成功。")
            print(f'当前账号存款余额为：{user_lib.get(username)["存款余额"]}')
    elif username not in user_lib.keys():
        print('账号不存在。')

# 转账模块
def transfer():
    while True:
        username1 = int(input("请输入转出账号："))
        username2 = int(input("请输入转入账号："))
        if username1 and username2 not in user_lib.keys():
            print("账号不对")
            continue
        password1 = input("请输入转出账号的密码：")
        if password1 != user_lib.get(username1)["密码"]:
            print("密码不对。")
        else:
            money = int(input("请输入转账的金额："))
            if money > user_lib.get(username1)["存款余额"]:
                print("钱不够。")
                continue
            user_lib.get(username1)["存款余额"] -= money
            user_lib.get(username2)["存款余额"] += money
            print("转账成功。")
            print(f'转出账号存款余额为：{user_lib.get(username1)["存款余额"]}')
            print(f'转入账号存款余额为：{user_lib.get(username2)["存款余额"]}')
        break

# 查询账户功能
def select_user():
    username = int(input("请输入要查询的账号："))
    if username in user_lib.keys():
        while True:
            password = input("请输入密码：")
            if password == user_lib.get(username)["密码"]:
                print(f"当前账号：{username}，密码：{password}，余额：{user_lib.get(username)['存款余额']}，"
                      f"用户居住地址：{user_lib.get(username)['地址']}，当前账户的开户行：{user_lib.get(username)['开户行']}")
            else:
                print("密码错误，请重新输入。")
                continue
            break